<template>
    <div class="addr-add-container">
        <ul class="form">
            <li><input type="text" placeholder="姓名" v-model="addrInfo.userName"></li>
            <li><input type="text" placeholder="邮政编码" v-model="addrInfo.postalCode"></li>
            <li><input type="text" placeholder="省份" v-model="addrInfo.provinceName"></li>
            <li><input type="text" placeholder="城市" v-model="addrInfo.cityName"></li>
            <li><input type="text" placeholder="地区" v-model="addrInfo.countyName"></li>
            <li><input type="text" placeholder="详细地址" v-model="addrInfo.detailInfo"></li>
            <li style="text-align:center">
                <button type="primary" size="mini" @click="getAddr">一键获取微信地址</button>
            </li>
        </ul>
        <div class="actions">
            <button type="warn" @click="saveAddr">新增</button>
        </div>
    </div>
</template>
<script>
export default {
    data(){
        return {
            addrInfo:{
                userName:'',
                mobile:'',
                provinceName:'',
                cityName:'',
                countyName:'',
                detailInfo:'',
                telNumber:'',
                postalCode:''
            }
        }
    },
    methods:{
        chooseWxAddress(){
            const that=this;
            wx.chooseAddress({
                success(res) {
                    that.addrInfo.userName=res.userName;
                    that.addrInfo.provinceName=res.provinceName;
                    that.addrInfo.cityName=res.cityName;
                    that.addrInfo.countyName=res.countyName;
                    that.addrInfo.detailInfo=res.detailInfo;
                    that.addrInfo.telNumber=res.telNumber;
                    that.addrInfo.postalCode=res.postalCode;
                }
            });
        },
        getAddr() {
            let that=this;
            wx.getSetting({
                success(res) {
                    // 有权限, 直接获取地址信息
                    if (res.authSetting["scope.address"]) {
                        that.chooseWxAddress();
                    } else { //暂时没有权限
                        // 向用户发起授权请求
                        wx.authorize({
                            scope: "scope.address",
                            success() {
                                // 用户已经同意授权&&调用获取地址接口
                               that.chooseWxAddress();
                            }
                        });
                    }
                }
            });
        },
        saveAddr(){
            const that=this;
            // 从本地存储获取用户id
            wx.getStorage({
                key:'userInfo',
                success(res){
                    let data={
                        uid:res.data.id,
                        consignee:that.userName,
                        // mobile:'mobile',
                        province:that.provinceName,
                        city:that.cityName,
                        district:that.countyName,
                        address:that.detailInfo,
                        // telNumber:'telNumber',
                        zipcode:that.postalCode
                    }
                    that.$http.post({
                        url:'m=Index&a=addrAdd',
                        data:data
                    }).then((res)=>{
                        if(res.errcode==0){
                            wx.showToast({
                                title:'添加成功',
                                duration:1000,
                                success(){
                                    mpvue.navigateTo({url:'../addr_list/main'});
                                }
                            })    
                        }else{
                            console.log(res);
                        }
                        
                    });

                }
            });

            return;

            
        }
    }
}
</script>
<style lang="scss">
@import './static/styles/reset.scss';
page{
    background-color: $bgColor;
}
.addr-add-container{
    .form{
        background: #FFF;
        input{
            padding: 24rpx 20rpx;
            margin: 20rpx 0;
            border-bottom: 1px solid $bgColor;
        }
    }
    .actions{
        position: fixed;
        bottom:10rpx;
        left:0;
        width: 100%;
    }
}
</style>
   
